﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.Entity;
using System.Data.Entity.Infrastructure;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Web;
using System.Web.Http;
using MvcApplication1.Models;

namespace MvcApplication1.Controllers
{
    public class BookListController : ApiController
    {
        private BookListContext db = new BookListContext();

        // GET api/BookList
        public IEnumerable<BookList> GetBookLists()
        {
            return db.BookLists.Where(u => u.UserId == User.Identity.Name);
        }

        // GET api/BookList/5
        public BookList GetBookList(int id)
        {
            BookList booklist = db.BookLists.Find(id);
            if (booklist == null)
            {
                throw new HttpResponseException(Request.CreateResponse(HttpStatusCode.NotFound));
            }

            return booklist;
        }

        // PUT api/BookList/5
        public HttpResponseMessage PutBookList(int id, BookList booklist)
        {
            if (!ModelState.IsValid)
            {
                return Request.CreateErrorResponse(HttpStatusCode.BadRequest, ModelState);
            }

            if (id != booklist.BookListId)
            {
                return Request.CreateResponse(HttpStatusCode.BadRequest);
            }

            db.Entry(booklist).State = EntityState.Modified;

            try
            {
                db.SaveChanges();
            }
            catch (DbUpdateConcurrencyException ex)
            {
                return Request.CreateErrorResponse(HttpStatusCode.NotFound, ex);
            }

            return Request.CreateResponse(HttpStatusCode.OK);
        }

        // POST api/BookList
        public HttpResponseMessage PostBookList(BookList booklist)
        {
            if (ModelState.IsValid)
            {
                db.BookLists.Add(booklist);
                db.SaveChanges();

                HttpResponseMessage response = Request.CreateResponse(HttpStatusCode.Created, booklist);
                response.Headers.Location = new Uri(Url.Link("DefaultApi", new { id = booklist.BookListId }));
                return response;
            }
            else
            {
                return Request.CreateErrorResponse(HttpStatusCode.BadRequest, ModelState);
            }
        }

        // DELETE api/BookList/5
        public HttpResponseMessage DeleteBookList(int id)
        {
            BookList booklist = db.BookLists.Find(id);
            if (booklist == null)
            {
                return Request.CreateResponse(HttpStatusCode.NotFound);
            }

            db.BookLists.Remove(booklist);

            try
            {
                db.SaveChanges();
            }
            catch (DbUpdateConcurrencyException ex)
            {
                return Request.CreateErrorResponse(HttpStatusCode.NotFound, ex);
            }

            return Request.CreateResponse(HttpStatusCode.OK, booklist);
        }

        protected override void Dispose(bool disposing)
        {
            db.Dispose();
            base.Dispose(disposing);
        }
    }
}